package com.itheima.contorller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/10/29 18:08
 */

@RestController
@RequestMapping("/employee")
public class Employeecontroller {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 管理员后台登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        // 获取前端登录账号
        String username = employee.getUsername();
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        // 设置查询条件
        queryWrapper.eq("username", username);
        // 按username查询，
        Employee empl = employeeService.getOne(queryWrapper);
        // 判断是否查询到用户
        if(empl == null){
            return R.error("用户不存在！！！");
        }
        // 将用户输入的密码通过md5加密后于数据库密码对比
        String password = employee.getPassword();
        String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        // 判断密码是否正确
        if(!pwdMd5.equals(empl.getPassword())){
            return R.error("密码错误");
        }
        // 判断次账号的status状态为0就是禁用，为1就是启用
        if(empl.getStatus() == 0){
            return R.error("账户已经被禁用！！！");
        }
        // 将用户的登录信息存储到session中，后面判断用户是否登录
        session.setAttribute("employee", empl.getId());
        // 给前端返回R封装结果
        return R.success(empl);
    }

    @PostMapping("/logout")
    public R logOut(HttpSession session){
        session.removeAttribute("employee");
        return  R.success("退出成功！！");
    }

    /**
     * 展示员工管理
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R getPage(Integer page, Integer pageSize, String name, HttpSession session){
        IPage iPage = new Page(page, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(name),"name", name);
        IPage<Employee> page1 = employeeService.page(iPage, queryWrapper);
        return R.success(page1);
    }

    /**
     *
     * 修改员工的数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     *修改
     * @param employee
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("ok");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee){
        // 设置初是密码，并且进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean save = employeeService.save(employee);
        return R.success(save);
    }
}
